package com.xinxing.learning.auth.common.util;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;
import java.util.function.Consumer;

public class FastAutoGeneratorUtil {
    public static String dbUrl = "";
    public static String dbUser = "";
    public static String dbPassword = "";
    public static String projectPath = "";

    static {
        dbUrl = "jdbc:mysql://114.115.139.186:3306/guigu-auth?useUnicode=true&characterEncoding=utf8&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull";
        dbUser = "root";
        dbPassword = "g8CpuGiwkJhqeSbd1";
        // 获取当前资源路径 当前项目所在目录 /Users/jielingyang/IdeaProjects/i-workspace/xinxin-mybatis-plus-example
        projectPath = System.getProperty("user.dir");
    }

    /**
     * 数据库配置 {@link FastAutoGeneratorUtil#datasourceBuilder()}
     * 全局配置 {@link FastAutoGeneratorUtil#globalConfigConsumer()}
     * 包配置 {@link FastAutoGeneratorUtil#packageConfigConsumer()}
     * 策略配置 {@link FastAutoGeneratorUtil#strategyConfigConsumer()}
     * 模板配置 {@link FastAutoGeneratorUtil#templateEngine()}
     * 注入配置 {@link FastAutoGeneratorUtil#injectionConfigConsumer()}
     *
     * @param args
     * @see FastAutoGeneratorUtil#datasourceBuilder()
     */
    public static void main(String[] args) {
        // 数据库配置
        FastAutoGenerator.create(datasourceBuilder())
                // 全局配置
                .globalConfig(globalConfigConsumer())
                // 包配置
                .packageConfig(packageConfigConsumer())
                // 策略配置
                .strategyConfig(strategyConfigConsumer())
                // 模板配置
                .templateConfig(templateConfigConsumer())
                .templateEngine(templateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                // 注入配置
                // .injectionConfig(injectionConfigConsumer())
                .execute();
    }

    /**
     * 数据库配置
     * https://www.mybatis-plus.com/guide/generator-new.html#%E6%95%B0%E6%8D%AE%E5%BA%93%E9%85%8D%E7%BD%AE-datasourceconfig
     *
     * @return
     */
    public static DataSourceConfig.Builder datasourceBuilder() {
        return new DataSourceConfig.Builder(dbUrl, dbUser, dbPassword);
    }

    /**
     * 全局配置 是数据配置下的一个小节 所以地址是数据库配置的地址 往下翻一点就是
     * https://www.mybatis-plus.com/guide/generator-new.html#%E6%95%B0%E6%8D%AE%E5%BA%93%E9%85%8D%E7%BD%AE-datasourceconfig
     *
     * @return
     */
    public static Consumer<GlobalConfig.Builder> globalConfigConsumer() {
        return builder -> {
            builder.author("lantianbaiyun") // 设置作者
                    .disableOpenDir()
                    .enableSwagger()
                    .fileOverride()
                    .dateType(DateType.ONLY_DATE)
                    .commentDate("yyyy-MM-dd HH:mm:ss")
                    .outputDir(projectPath + "/spring-boot-example/spring-boot-auth/src/main/java") // 指定输出目录
            ;
        };
    }

    /**
     * 包配置
     * https://www.mybatis-plus.com/guide/generator-new.html#%E5%8C%85%E9%85%8D%E7%BD%AE-packageconfig
     *
     * @return
     */
    public static Consumer<PackageConfig.Builder> packageConfigConsumer() {
        return builder -> {
            builder.parent("com.xinxing.learning") // 设置父包名
                    .moduleName("auth") // 设置父包模块名
                    .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/spring-boot-example/spring-boot-auth/src/main/resources/mapper")); // 设置mapperXml生成路径
        };
    }

    /**
     * 策略配置
     * https://www.mybatis-plus.com/guide/generator-new.html#%E7%AD%96%E7%95%A5%E9%85%8D%E7%BD%AE-strategyconfig
     * https://blog.csdn.net/qq_42682745/article/details/120626012
     *
     * @return
     */
    public static Consumer<StrategyConfig.Builder> strategyConfigConsumer() {
        return builder -> {
            builder.addInclude("sys_user") // 设置需要生成的表名
                    // .addTablePrefix("sys_") // 设置过滤表前缀
                    .serviceBuilder()
                    .formatServiceFileName("%sService")
                    .formatServiceImplFileName("%sServiceImpl")
                    .entityBuilder()
                    .enableTableFieldAnnotation()
                    .enableLombok()
            ;
        };
    }

    public static Consumer<TemplateConfig.Builder> templateConfigConsumer() {
        return builder -> {
            // 实体类使用我们自定义模板
            builder.entity("templates/myentity.java");
        };
    }

    /**
     * 模板配置
     * https://www.mybatis-plus.com/guide/generator-new.html#%E6%A8%A1%E6%9D%BF%E9%85%8D%E7%BD%AE-templateconfig
     *
     * @return
     */
    public static AbstractTemplateEngine templateEngine() {
        return new FreemarkerTemplateEngine();
    }

    /**
     * 注入配置
     * https://www.mybatis-plus.com/guide/generator-new.html#%E6%B3%A8%E5%85%A5%E9%85%8D%E7%BD%AE-injectionconfig
     *
     * @return
     */
    public static Consumer<InjectionConfig.Builder> injectionConfigConsumer() {
        return builder -> {
            builder.beforeOutputFile((tableInfo, objectMap) -> {
                        System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
                    })
                    .customMap(Collections.singletonMap("test", "baomidou"))
                    .customFile(Collections.singletonMap("test.txt", "/templates/test.vm"));
        };
    }
}
